package com.ybkj.daijia.server;

import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.event.model.errand.ErrandOrderCallBackEvent;
import com.ybkj.daijia.server.event.model.freight.FreightOrderCallBackEvent;
import com.ybkj.daijia.server.event.model.zhuan.ZhuanOrderCallBackEvent;
import com.ybkj.daijia.server.event.model.zhuanxian.ZhuanxianOrderCallBackEvent;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.ErrandService;
import com.ybkj.daijia.server.service.FreightService;
import com.ybkj.daijia.server.service.OrderService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.service.ZhuanxianService;
import java.util.TimerTask;
import org.springframework.context.event.ApplicationEventMulticaster;

public class OrderRevokeTimerTask extends TimerTask {

    private ApplicationEventMulticaster applicationEventMulticaster;

    private Long orderId;

    private String orderType;


    public OrderRevokeTimerTask(Long orderId, String orderType,
        ApplicationEventMulticaster applicationEventMulticaster) {
        this.orderId = orderId;
        this.orderType = orderType;
        this.applicationEventMulticaster = applicationEventMulticaster;
    }


    @Override
    public void run() {
//      if(orderType.equals("daijia")){
//        try {
//            OrderService orderService = Config.getWac().getBean(OrderService.class);
//            Order order = orderService.findOne(orderId);
//            if(null!=order){
//              if(order.getStatus().equals(Order.ORDER_STATUS_YIPAIDAN)){
//                DriverService driverService = Config.getWac().getBean(DriverService.class);
//                Driver driver = driverService.findOne(order.getDriverId());
//                Result<Order> result = orderService.autoCallback(order,driver);
//                if(result.isSuccess()){
//                  
//                }
//              }
//            }
//        } catch (Exception e1) {
//            e1.printStackTrace();
//        }
//    }
        if (orderType.equals("daijia")) {
            try {
                OrderService orderService = Config.getWac().getBean(OrderService.class);
                Order order = orderService.findOne(orderId);
                if (null != order) {
                    if (order.getStatus() == Order.ORDER_STATUS_YIPAIDAN
                        || order.getStatus() == Order.ORDER_STATUS_YUPAIDAN) {
//                  DriverService driverService = Config.getWac().getBean(DriverService.class);
//                  Driver driver = driverService.findOne(order.getDriverId());
                        orderService.revoke(order, "时间逾期，系统自动收回了该订单");

                        // OrderPaidanEvent paidanEvent = new OrderPaidanEvent(order);
                        //applicationEventMulticaster.multicastEvent(paidanEvent);

                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } else if (orderType.equals("zhuanche")) {
            try {
                ZhuancheService zhuancheService = Config.getWac().getBean(ZhuancheService.class);
                com.ybkj.daijia.server.zhuan.Order order = zhuancheService.findOne(orderId);
                if (null != order) {
                    if (order.getStatus()
                        .equals(com.ybkj.daijia.server.zhuan.Order.ORDER_STATUS_PAIDAN)) {
                        DriverService driverService = Config.getWac().getBean(DriverService.class);
                        Driver driver = driverService.findOne(order.getEmployId());
                        Result<com.ybkj.daijia.server.zhuan.Order> result = zhuancheService
                            .autoCallback(order, driver);
                        if (result.isSuccess()) {
                            ZhuanOrderCallBackEvent callBackEvent = new ZhuanOrderCallBackEvent(
                                order, driver);
                            applicationEventMulticaster.multicastEvent(callBackEvent);
                        }
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } else if (orderType.equals("errand")) {
            try {
                ErrandService errandService = Config.getWac().getBean(ErrandService.class);
                com.ybkj.daijia.server.errand.Order order = errandService.findOne(orderId);
                if (null != order) {
                    if (order.getStatus()
                        .equals(com.ybkj.daijia.server.errand.Order.ORDER_STATUS_PAIDAN)) {
                        DriverService driverService = Config.getWac().getBean(DriverService.class);
                        Driver driver = driverService.findOne(order.getEmployId());
                        Result<com.ybkj.daijia.server.errand.Order> result = errandService
                            .autoCallback(order, driver);
                        if (result.isSuccess()) {
                            ErrandOrderCallBackEvent callBackEvent = new ErrandOrderCallBackEvent(
                                order, driver);
                            applicationEventMulticaster.multicastEvent(callBackEvent);
                        }
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } else if (orderType.equals("freight")) {
            try {
                FreightService freightService = Config.getWac().getBean(FreightService.class);
                com.ybkj.daijia.server.freight.Order order = freightService.findOne(orderId);
                if (null != order) {
                    if (order.getStatus()
                        .equals(com.ybkj.daijia.server.freight.Order.ORDER_STATUS_PAIDAN)) {
                        DriverService driverService = Config.getWac().getBean(DriverService.class);
                        Driver driver = driverService.findOne(order.getEmployId());
                        Result<com.ybkj.daijia.server.freight.Order> result = freightService
                            .autoCallback(order, driver);
                        if (result.isSuccess()) {
                            FreightOrderCallBackEvent callBackEvent = new FreightOrderCallBackEvent(
                                order, driver);
                            applicationEventMulticaster.multicastEvent(callBackEvent);
                        }
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } else if (orderType.equals("zhuanxian")) {
            try {
                ZhuanxianService zhuanxianService = Config.getWac().getBean(ZhuanxianService.class);
                com.ybkj.daijia.server.zhuanxian.Order order = zhuanxianService.findOne(orderId);
                if (null != order) {
                    if (order.getStatus()
                        .equals(com.ybkj.daijia.server.zhuanxian.Order.ORDER_STATUS_PAIDAN)) {
                        DriverService driverService = Config.getWac().getBean(DriverService.class);
                        Driver driver = driverService.findOne(order.getEmployId());
                        Result<com.ybkj.daijia.server.zhuanxian.Order> result = zhuanxianService
                            .autoCallback(order, driver);
                        if (result.isSuccess()) {
                            ZhuanxianOrderCallBackEvent callBackEvent = new ZhuanxianOrderCallBackEvent(
                                order, driver);
                            applicationEventMulticaster.multicastEvent(callBackEvent);
                        }
                    }
                }
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

}
